package org.me.tramdroid;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class XmlHandler extends DefaultHandler{
    private boolean in_SoapEnv = false;
    private boolean in_SoapBody = false;
    private boolean in_GetAddSuggRes = false;
    private boolean in_GetAddSuggResp = false;
    private String SoapBody = "soap:Body";
    private String GetAddSuggRes = "GetAddressesSuggestionsResult";
    private String GetAddSuggResp = "GetAddressesSuggestionsResponse xmlns=\"http://www.vasttrafik.se/\"";
    private String SoapEnv = "soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/";
       private ParsedDataSet dataSet = new ParsedDataSet();

     // ===========================================================
     // Getter & Setter
     // ===========================================================

     public ParsedDataSet getParsedData() {
          return this.dataSet;
     }

     // ===========================================================
     // Methods
     // ===========================================================
     @Override
     public void startDocument() throws SAXException {
          this.dataSet = new ParsedDataSet();
     }

     @Override
     public void endDocument() throws SAXException {
          // Nothing to do
     }

     /** Gets be called on opening tags like:
      * <tag>
      * Can provide attribute(s), when xml was like:
      * <tag attribute="attributeValue">*/
     @Override
     public void startElement(String namespaceURI, String localName,
               String qName, Attributes atts) throws SAXException {
          if (localName.equals(SoapEnv)) {
               this.in_SoapEnv = true;
          }else if (localName.equals(SoapBody)) {
               this.in_SoapBody = true;
          }else if (localName.equals(GetAddSuggResp)) {
               this.in_GetAddSuggResp = true;
          }else if (localName.equals(GetAddSuggRes)) {
              this.in_GetAddSuggRes = true;
              // Extract an Attribute
          }
     }

     /** Gets be called on closing tags like:
      * </tag> */
     @Override
     public void endElement(String namespaceURI, String localName, String qName)
               throws SAXException {
               if (localName.equals(SoapEnv)) {
               this.in_SoapEnv = false;
          }else if (localName.equals(SoapBody)) {
               this.in_SoapBody = false;
          }else if (localName.equals(GetAddSuggResp)) {
               this.in_GetAddSuggResp = false;
          }else if (localName.equals(GetAddSuggRes)) {
              this.in_GetAddSuggRes = false;
              // Extract an Attribute
          }
     }
     
     /** Gets be called on the following structure:
      * <tag>characters</tag> */
     @Override
    public void characters(char ch[], int start, int length) {
          if(this.in_GetAddSuggRes){
          dataSet.setExtractedString(new String(ch, start, length));
     }
    }

}